<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!-- 主体内容 -->
<div class="layui-fluid" >



    <form class="layui-form" id="form1">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-card-header"  style="color:#d02e25;font-family: '楷体'; ">
                    添加学员信息
                </div>


                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name"  lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>

                    <label class="layui-form-label">居住地</label>
                    <div class="layui-input-inline">
                        <input type="text" name="livein"  lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>

                    <label class="layui-form-label">所属党支部</label>
                    <div class="layui-input-inline">
                        <div class="layui-inline" style="width: 100%;">

                            <div class="layui-input-inline" style="width: 100%;">
                                <select name="branch" id="branch" lay-verify="required" lay-search="" style="width: 100%;">

                                </select>
                            </div>
                        </div>
                    </div>

                </div>

                <%--<label class="layui-form-label"></label>--%>
                <div class="layui-form-item">
                    <label class="layui-form-label">选择要修的课程</label>
                    <div class="layui-input-block" id="lessons">
                        <%--<input type="checkbox" name="typeOne" id="typeOne" title="是否属于一类">--%>
                        <%--<input type="checkbox" name="typeTwo" id="typeTwo" title="是否属于二类">--%>
                        <%--<input type="checkbox" name="status" id="status" title="是否开设">--%>

                    </div>
                </div>





                <div class="layui-form-item" style="margin-left: 440px;">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="ok">提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary" id="reset">重置</button>
                    </div>
                </div>


            </div>
        </div>
    </form>


</div>



<script type="application/javascript">
    // layui.use(['form','laydate'], function(){

    var form = layui.form;
    var checkSize;


    //提交信息
    form.on('submit(ok)',function (data) {
        console.log(data.field);
        //组装数据
        var studentData = {};
        studentData.name = data.field.name;
        studentData.livein = data.field.livein;
        studentData.branch = data.field.branch;

        var lessonData = new Array();
        for(var i=0;i<checkSize;i++){
            var c = document.getElementById("lesson"+i).checked;
            var v = document.getElementById("lesson"+i).value;
            if(c==true){
                var temp = lessonData.length;
                lessonData[temp] = v;
            }

        }

        console.log(lessonData);

        $.ajax({
            type:"post",
            url:"/ajax/party/admin/addstudent",
            contentType : 'application/json;charset=utf-8',
            //设置请求头信息
            dataType:"json",
            data:JSON.stringify(studentData),
            success:function (res) {
                if(res.code==0){
                    // layer.alert(checkSize);
                    var time = res.time;
                    var len = lessonData.length;
                    lessonData[len] = time;
                    $.ajax({
                        type:"post",
                        url:"/ajax/party/admin/addSlesson",
                        contentType : 'application/json;charset=utf-8',
                        //设置请求头信息
                        dataType:"json",
                        data:JSON.stringify(lessonData),
                        success:function (res) {
                            if(res.code==0){
                                layer.alert("ok");
                            }else {
                                layer.alert("lazha");
                            }
                        }
                    })

                }else {
                    layer.alert("发生了一些小错误，提交失败！");
                }
            }
        })

        return false;
    })
    // });

</script>

<script>
    //显示搜索选择框

    $.post("/ajax/party/admin/branches",{
        check:1024
    },function (res) {
        if(res.code==0){
            //得到所有级别
            var names = new Array();
            for(var i=0;i<res.data.length;i++){
                names[i]=res.data[i].branchName;
            }
            //将等级动态显示到界面上
            for(var i=0;i<names.length;i++){
                document.getElementById("branch").options.add(new Option(names[i],names[i]));

            }

        }
        else{
            layer.alert("未知错误！");
        }
        form.render();
    });
    $.post("/ajax/party/admin/lessons",{
        check:1024
    },function (res) {
        if(res.code==0){
            console.log(res);
            // layer.alert("ok");
            var names = new Array();
            var ids = new Array();
            for(var i=0;i<res.data.length;i++){
                names[i] = res.data[i].name;
                ids[i] = res.data[i].id;
            }
            checkSize = names.length;
            console.log(names);
            //将课程名字动态显示到页面上
            for(var i=0;i<names.length;i++){
                $("#lessons").append(' <input type="checkbox" value='+ ids[i] +' name="lesson'+ i +'" id="lesson'+ i +'" title="'+ names[i] +'">');
            }
            form.render();
        }
    })

</script>



